﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace JiveMessenger.ViewModels
{
    public class ViewModelChatWindow : ViewModelBase
    {
        private IClient _currentClient;

        public RelayCommand OpenOptionsCommand { get; set; }
        public RelayCommand ViewLogCommand { get; set; }
        public RelayCommand<KeyEventArgs> PreviewKeyUpCommand { get; private set; }
        public RelayCommand<MouseButtonEventArgs> HideMenuCommand { get; private set; }
        public RelayCommand EditFontCommand { get; set; }
        public RelayCommand StartUpdateCheckCommand { get; set; }
        public RelayCommand ShowAboutWindowCommand { get; set; }
        public RelayCommand OpenWelcomeTabCommand { get; set; }

        private ViewModelChat _currentActiveViewModel;
        public ViewModelChat CurrentActiveViewModel
        {
            get { return _currentActiveViewModel; }
            set
            {
                if (_currentActiveViewModel != value)
                {
                    Set(() => CurrentActiveViewModel, ref _currentActiveViewModel, value);
                }
            }
        }


        private string _windowTitle;
        public string WindowTitle
        {
            get { return _windowTitle; }
            set
            {
                if (_windowTitle != value)
                {
                    Set(() => WindowTitle, ref _windowTitle, value);
                }
            }
        }

        private bool _isMainMenuVisible;
        public bool IsMainMenuVisible
        {
            get { return _isMainMenuVisible; }
            set
            {
                if (_isMainMenuVisible != value)
                {
                    Set(() => IsMainMenuVisible, ref _isMainMenuVisible, value);
                }
            }
        }

        public ViewModelChatWindow(IClient currentClient)
        {
            _currentClient = currentClient;
            this.WindowTitle = string.Empty;
            this.OpenOptionsCommand = new RelayCommand(OpenOptionsCommandAction, () => _currentClient.IsLoggedIn);
            this.ViewLogCommand = new RelayCommand(ViewLogCommandAction, () => _currentClient.IsLoggedIn);
            this.PreviewKeyUpCommand = new RelayCommand<KeyEventArgs>(PreviewKeyUpCommandAction);

            this.HideMenuCommand = new RelayCommand<MouseButtonEventArgs>(HideMenuCommandAction);
            this.EditFontCommand = new RelayCommand(EditFontCommandAction, () => _currentClient.IsLoggedIn);
            this.ShowAboutWindowCommand = new RelayCommand(ShowAboutWindowCommandAction);
        }

        private void ShowAboutWindowCommandAction()
        {
            Messenger.Default.Send(new NotificationMessage(this, "OpenAboutWindow"));
        }
        private void EditFontCommandAction()
        {
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "ChangeFont"));
        }

        private void HideMenuCommandAction(MouseButtonEventArgs obj)
        {
            IsMainMenuVisible = false;
        }

        private void PreviewKeyUpCommandAction(KeyEventArgs e)
        {
            if ((e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt) && Properties.Settings.Default.SettingEnableMainMenu)
            {
                IsMainMenuVisible = !IsMainMenuVisible;
            }
        }


        private void ViewLogCommandAction()
        {
            CurrentActiveViewModel.WriteLog();

            Messenger.Default.Send(new NotificationMessage(this, "OpenLogs"));
        }
        private void OpenOptionsCommandAction()
        {
            Messenger.Default.Send(new NotificationMessage(this, "OpenOptions"));
        }

        public void RunOnNetworkDisconnect()
        {
            this.CurrentActiveViewModel.IsInputTextEnabled = false;
        }
        public void RunOnNetworkConnect()
        {
            this.CurrentActiveViewModel.IsInputTextEnabled = true;
        }

        public virtual void SetStatus(bool offline)
        {
            CurrentActiveViewModel.SetStatus(offline);
            if (offline)
            {
                this.WindowTitle = string.Format("{0} -Offline-", this.CurrentActiveViewModel.ViewModelName);
            }
            else
            {
                this.WindowTitle = this.CurrentActiveViewModel.ViewModelName;
            }
        }
    }
}
